package com.cynteka.cyfoman.ui;

import com.cynteka.cyfoman.core.config.ActionMD;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.List;

public class FolderChangedDlg extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JList newFilesList;
    private JPanel actionPanel;
    private DefaultListModel model = new DefaultListModel();
    private List<ActionMD> ations;
    private ActionMD selectedAction;

    public FolderChangedDlg() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        setTitle("Новые файлы");
        ListSelectionListener listener = new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent listSelectionEvent) {
                updateEnabledStatus();
            }
        };
        newFilesList.getSelectionModel().addListSelectionListener(listener);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

// call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

// call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    }

    private void onOK() {
// add your code here
        // dispose();
        setVisible(false);
    }

    private void onCancel() {
// add your code here if necessary
        // dispose();
    }

    public static void main(String[] args) {
        FolderChangedDlg dialog = new FolderChangedDlg();
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }

    public File[] getSelectedFiles() {
        Object[] oFiles = newFilesList.getSelectedValues();
        File[] res = new File[oFiles.length];
        int i = 0;
        for (Object oFile : oFiles) {
            FileWrapper fw = (FileWrapper) oFile;
            res[i] = fw.file;
            i++;
        }
        return res;
    }

    public ActionMD getSelectedAction() {
        return selectedAction;
    }

    private static class FileWrapper {
        private File file;

        public FileWrapper(File f) {
            this.file = f;
        }

        public String toString() {
            return file.getName();
        }
    }

    public void addFiles(File[] newFiles) {
        int[] selected = newFilesList.getSelectedIndices();
        int oldSize = model.getSize();
        for (File f : newFiles) {
            model.addElement(new FileWrapper(f));
        }
        newFilesList.setModel(model);
        int[] newSelected = new int[selected.length + newFiles.length];
        System.arraycopy(selected, 0, newSelected, 0, selected.length);
        for (int i = 0; i < newFiles.length; i++) {
            newSelected[selected.length + i] = oldSize + i;
        }
        newFilesList.setSelectedIndices(newSelected);

    }

    public List<ActionMD> getAtions() {
        return ations;
    }

    public void setAtions(List<ActionMD> actions) {
        this.ations = actions;
        actionPanel.setLayout(new GridLayout(actions.size(), 1));
        ButtonGroup bg = new ButtonGroup();
        for (final ActionMD action : actions) {
            final JRadioButton rb = new JRadioButton(action.getName());
            rb.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent changeEvent) {
                    if (rb.isSelected()) {
                        selectedAction = action;
                        updateEnabledStatus();
                    }
                }
            });
            actionPanel.add(rb);
            bg.add(rb);
        }
    }

    protected void updateEnabledStatus() {
        buttonCancel.setEnabled(false);
        boolean okEnabled = true;
        if (okEnabled)
            if (selectedAction == null)
                okEnabled = false;
        if (okEnabled)
            if (newFilesList.getSelectedIndices().length == 0)
                okEnabled = false;
        buttonOK.setEnabled(okEnabled);
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        updateEnabledStatus();
    }
}
